package com.gy.hsim.netty.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

import javax.swing.JOptionPane;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gy.hsim.netty.proto.BserviceProto;


public class NettyClient {

    public static void main(String[] args) throws Exception{
//        new SimpleClient("localhost", 8080).run();
    }

    private final String host;
    private final int port;
    public static Channel channel;

    public NettyClient(String host, int port){
        this.host = host;
        this.port = port;
    }

    public void run() throws Exception{
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap  = new Bootstrap()
                    .group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new NettyClientInitializer());
            channel = bootstrap.connect(host, port).sync().channel();
            
            
//            BserviceProto.BserviceEntity.Builder entity = BserviceProto.BserviceEntity.newBuilder();
//        	entity.setImSystem("imSystem");
//        	entity.setToUid("06007010000000020151231");
//        	entity.setIfOperator("Y");
//        	entity.setPageNum("1");
//        	entity.setPageSize("2");
//
//        	JOptionPane.showMessageDialog(null, "开始发送消息:ImSystem:" + entity.build().getImSystem(), "开始发送消息" ,JOptionPane.ERROR_MESSAGE);
//        	channel.writeAndFlush(entity.build());
            
            
            channel.closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }

    }

    private static final Logger log = LoggerFactory
			.getLogger(NettyClient.class);
    
    
    public void sendMsg(String msg) throws Exception{
    	BserviceProto.BserviceEntity.Builder entity = BserviceProto.BserviceEntity.newBuilder();
    	entity.setImSystem("imSystem");
    	entity.setToUid("06007010000000020151231");
    	entity.setIfOperator("Y");
    	entity.setPageNum("1");
    	entity.setPageSize("2");
    	
    	entity.setImSystem(msg);
    	
//    	byte[] s = entity.build().toByteArray();
//    	byte[] scount = new byte[]{(byte)(s.length / 100),(byte)(s.length / 10),(byte)(s.length % 10)};
//    	s = byteMerger(s,scount);
//    	System.out.println(s.length + "....length");
//    	for (int i = 0; i < s.length; i++) {
//			System.out.print(s[i]);
//		}
    	JOptionPane.showMessageDialog(null, "开始发送消息:ImSystem:" + entity.build().getImSystem(), "开始发送消息" ,JOptionPane.ERROR_MESSAGE);
    	channel.writeAndFlush(entity.build().toByteArray());

    }
    
    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2){  
        byte[] byte_3 = new byte[byte_1.length+byte_2.length];  
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);  
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);  
        return byte_3;  
    } 
}
